package com.chasmcity.munrobagging;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.chasmcity.munrobagging.model.Munro;
import com.chasmcity.munrobagging.model.Type;

/**
 * TODO: Missing class description for MunroDetailsActivity!
 * <p>
 * &copy; Copyright 2011 Kizoom Ltd.
 * <p>
 * $Id: wildebeest_standard.epf,v 1.9 2007/01/17 14:59:59 danf Exp $
 */
public class MunroDetailsActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.munro_details);
		
		((Button) findViewById(R.id.showOnMap)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				MunroMapActivity.launch(MunroDetailsActivity.this, getMunro());
			}
		});
		
		Button walkHighlandsButton = (Button) findViewById(R.id.walkHighlands);
		if (getMunro().getType() == Type.MUNRO) {
			walkHighlandsButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setData(createWalkHighlandsUrl());
					startActivity(intent);
				}
			});

		} else {
			walkHighlandsButton.setVisibility(View.GONE);
		}
		populateDetails();
	}

	private Munro getMunro() {
		return (Munro) getIntent().getExtras().getSerializable(Munro.class.getName());
	}
	
	private void populateDetails() {
		Munro munro = getMunro();
		
		((TextView)findViewById(R.id.munroName)).setText(munro.getName());
		
		if (munro.getType() != null) {
			((TextView)findViewById(R.id.munroType)).setText(munro.getType().toString());
		}
		
		((TextView)findViewById(R.id.munroHeight)).setText(String.format("%s metres, %s feet", munro.getHeightInMetres(), munro.getHeightInFeet()));
		
		if (munro.getSection() != null) {
			((TextView)findViewById(R.id.munroSection)).setText(munro.getSection().toString());
		}
		
		if (munro.getMapReference() != null) {
			((TextView)findViewById(R.id.munroMapRef)).setText(munro.getMapReference().toString());
		}
		
		if (munro.getLatLong() != null) {
			((TextView)findViewById(R.id.munroLatLong)).setText(String.format("%.6f, %.6f", munro.getLatLong().getLatitude(), munro.getLatLong().getLongitude())); 
		}
	}
	
	private Uri createWalkHighlandsUrl() {
		return Uri.parse(getString(R.string.walkHighlands_linkTemplate, getMunro().getName().replace('\'', '-').replace(' ', '-').toLowerCase()));
	}

	static void launch(Context context, Munro munro) {
		Intent intent = new Intent(context, MunroDetailsActivity.class);
		intent.putExtra(Munro.class.getName(), munro);
		context.startActivity(intent);
	}
}
